package com.es.disped.web.controller.oj.users;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.oj.user.HustUserService;
import com.es.disped.common.annotation.DynamicDataSource;
import com.es.disped.common.model.MsgModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.User;
import com.es.disped.dao.oj.model.Users;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月17日 下午11:59:52<br>
 *  名称：UsersController.java<br>
 *  描述：OJ用户管理<br>
 */
@Api("OJ用户管理")
@Controller
@RequestMapping("/oj/users")
public class UsersController extends BaseController{

	
	@Autowired
	HustUserService hustUserService;
	
	@ApiOperation(value="OJ注册",httpMethod="POST",response=MsgModel.class,notes="OJ注册")
	@RequestMapping(value="/registe",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel RegisteOJ()
	{
		int count=0;
		User user=(User) this.getSession().getAttribute("registeUser");
		Users ojUser=hustUserService.selectByPrimaryKey(user.getUserId());
		if(this.isNull(ojUser))
		{
			ojUser=new Users();
			ojUser.setUserId(user.getPhone());
			ojUser.setNick(user.getUsername());
			ojUser.setPassword(user.getPassword());
			ojUser.setEmail(user.getEmail());
			ojUser.setSchool(user.getBelongSchool());
			ojUser.setIp(this.getRequest().getRemoteAddr());
			ojUser.setRegTime(new Date());
			ojUser.setAccesstime(new Date());
			count=hustUserService.insertSelective(ojUser);
		}
		this.getSession().removeAttribute("registeUser");
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),count==1?"":"OJ系统注册失败");
	}
	
	
	@ApiOperation(value="统计用户答题情况",response=MsgModel.class,httpMethod="POST",
	notes="通过用户ID统计用户答题情况，此处用户ID为ESH平台用户注册的手机号")
	@RequestMapping(value="/getUserACRate",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel getUserACRate(@RequestBody Users user)
	{
		Map<String, Object> map=new HashMap<String, Object>();
		Users res=null;
		if(!this.isNull(user.getUserId()))
		{
			res=hustUserService.selectByPrimaryKey(user.getUserId());
		}
		map.put("problemSum", res.getSubmit());
		map.put("passSum", res.getSolved());
		map.put("rate", new StringBuilder(res.getSubmit()>0?String.format("%.2f", 100.0*res.getSolved()/res.getSubmit()):"0.00")+"%");
		return this.resultMsg(HttpStatus.OK.value(),"",map);
	}
}
